import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Form, Select } from 'antd'
import _ from 'loadsh'
import { Line, Cell } from '@components/ApprovePre/Approve'

const { Item } = Form
const { Option } = Select
export default class StepOneInput extends Component {
  state = {
    keyTitleMap: {},
    fileList: []
  }

  initKeyTitleMap () {
    const map = {}
    const list = _.get(this.props.instance || {}, 'currentStep.vars', [])
    list.forEach(li => {
      map[li.key] = li.title
    })
    this.setState({
      keyTitleMap: map
    })
  }

  // 改变文书回调
  changeDoc (v) {
    const c = this.props.formRef.current
    c.setFieldsValue({
      title: this.props.docTemplateMap[v].title,
      code: v
    })
  }

  componentDidUpdate (prev) {
    if (this.props.instance !== prev.instance) {
      this.initKeyTitleMap()
    }
  }

  componentDidMount () {
    this.initKeyTitleMap()
  }

  render () {
    const { keyTitleMap } = this.state
    return (
      <div>
        <Line>
          <Cell>
            <Item labelCol={{ flex: '108px' }} label='操作者'>
              <span>{this.props.userInfo.realname}</span>
            </Item>
          </Cell>
        </Line>
        <Line>
          <Cell style={{ width: '65%' }}>
            <Item noStyle name='title'><span /></Item>
            <Item labelCol={{ flex: '108px' }} name={['code']} label={keyTitleMap.case_document_name} rules={[{ required: true, message: `${keyTitleMap.case_document_name}不能为空` }]}>
              <Select placeholder={`请输入${keyTitleMap.case_document_name}`} onChange={v => this.changeDoc(v)}>
                {this.props.docTemplates.map(li => {
                  return <Option key={li.id} value={li.code}>{li.title}</Option>
                })}
              </Select>
            </Item>
          </Cell>
        </Line>
      </div>
    )
  }
}

StepOneInput.proptypes = {
  caseDetail: PropTypes.object,
  userInfo: PropTypes.object,
  instance: PropTypes.object,
  formRef: PropTypes.object,
  docTemplates: PropTypes.array,
  docTemplateMap: PropTypes.object
}

StepOneInput.defaultProps = {
  caseDetail: {},
  userInfo: {},
  instance: {},
  formRef: {},
  docTemplates: [],
  docTemplateMap: {}
}
